package cn.jdy.plugins.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.entity.ServerPoint;

/**
 * @title 邮政服务网点的信息操作实现
 * @author yangyingming
 * @since 19-11-2015
 */
@Service
public class ServerPointService extends BaseDao<ServerPoint>{

	@Autowired
	private ConfService confService;
	
	/**
	 * 按条件查询
	 * @param sp
	 * 
	 * @return
	 */
	public List<ServerPoint> query(ServerPoint sp){
		if(sp==null) return super.findAll();
		if(sp.getiDisplayLength() > 0){
			  return super.find(createFilter(sp),sp.getiDisplayStart(),sp.getiDisplayLength());
			}
		return super.find(createFilter(sp));
	}
	
	public Long count(ServerPoint sp){
		return super.count(createFilter(sp));
	}
	/**
	 * 拼装条件
	 * @param sp
	 * 
	 * @return
	 */
	private DetachedCriteria createFilter(ServerPoint sp){
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		if(!StringUtil.isNullOrEmpty(sp.getName())) {
	        dc.add(Restrictions.like("name", sp.getName(),MatchMode.ANYWHERE));
		} 
		if(!StringUtil.isNullOrEmpty(sp.getNode())) {
			dc.add(Restrictions.eq("node", sp.getNode()));
	    }
		if(!StringUtil.isNullOrEmpty(sp.getArea())) {
			dc.add(Restrictions.eq("area", sp.getArea()));
	    }
		dc.addOrder(Order.desc("createTime"));
			return dc;
	}

}
